<?php
/*
 +----------------------------------------------------------------------
 + Title        : Article 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2021/3/2 6:57
 + Last-time    : 2021/3/2 6:57+ chen2
 + Desc         : Article
 +----------------------------------------------------------------------
*/


namespace app\admin\controller;


use app\common\service\ArticleService;
use app\common\validate\ArticleValidate;
use think\App;

use function get_client_id;

class ArticleController extends AdminBaseController
{
    protected $service;
    protected $validate;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->service = new ArticleService();
        $this->validate = new ArticleValidate();
    }

    public function index()
    {
        $data = $this->getParams();
        $this->validate->extractScene($data, 'index')
                       ->failException(true)
                       ->check($data);
        $result = $this->service->getArticleList($data);
        return $this->returnSuccess($result);
    }

    /**
     * @throws \Exception
     */
    public function save()
    {
        $data = $this->getParams();
        $data['admin_uuid'] = get_client_id();
        $this->validate->extractScene($data, 'save')
                       ->failException(true)
                       ->check($data);
        $res = $this->service->addArticleItem($data);
        return $this->returnSuccess($res);
    }

    public function update()
    {
        $data = $this->getParams();
        $this->validate->extractScene($data, 'update')
                       ->failException(true)
                       ->check($data);
        $res = $this->service->setArticleItem($data);
        return $this->returnSuccess($res);
    }

    public function read()
    {
        $data = $this->getParams();
        $this->validate->extractScene($data, 'read')
                       ->failException(true)
                       ->check($data);
        $result = $this->service->getArticleItem($data['article_id']);
        return $this->returnSuccess($result);
    }

    public function delete()
    {
        $data = $this->getParams();
        $res = $this->service->delArticleItem($data['article_id']);
        return $this->returnSuccess($res);
    }
}